<?php
/**
 * Controller is the customized base controller class.
 * All controller classes for this application should extend from this base class.
 */
class Controller extends CController
{
	/**
	 * @var string the default layout for the controller view. Defaults to '//layouts/column1',
	 * meaning using a single column layout. See 'protected/views/layouts/column1.php'.
	 */
	public $layout='//layouts/column1';
	/**
	 * @var array context menu items. This property will be assigned to {@link CMenu::items}.
	 */
	public $menu=array();
	/**
	 * @var array the breadcrumbs of the current page. The value of this property will
	 * be assigned to {@link CBreadcrumbs::links}. Please refer to {@link CBreadcrumbs::links}
	 * for more details on how to specify this property.
	 */
	public $breadcrumbs=array();
    public $isAdmin = 0;
	public $activateArray = array(null,null,null,null,null);
	public $isActivated = -1;
    
	// 学生通知的数目 
 	protected $personalNoNum = 0; // 个人通知
 	protected $publicNoNum = 0;   // 公共通知，暂时不做,没有必要
	
	
	//判断是否登陆
	public function beforeAction($action){
	  //var_dump(Yii::app()->user->isGuest);die();
	  if(Yii::app()->user->isGuest)
	   $this->redirect(Yii::app()->createUrl('login/index'));
	  else{
			if(Yii::app()->user->isNew == '1'){
				$this->redirect(Yii::app()->createUrl('login/resetpassword'));
			}else{
				$this->getNotificationNum();
				
			}
			return true;
			
      }     
       
	}
	protected function getNotificationNum(){
		if(Yii::app()->user->rights == '1'){
			$ci = new CDbCriteria;
			$ci->addCondition('uId = '.Yii::app()->user->getId());
			$ci->addCondition('isRead = 0');
			$this->personalNoNum  = Stunotification::model()->count($ci);
			
		}
		
	}
	//获取当前的控制器，来确定左侧的菜单的状态
	public function getCurrentController(){
		$pathArray = explode('/',Yii::app()->request->getPathInfo());
		if($pathArray[0] === ''){
			if(Yii::app()->user->rights === '1')
				return 'stubasicinfo';
			else
			    return 'basicinfo';
		}else{
			
			return $pathArray[0];
		}
		
	}
	// 获取当前的方法
	public function getCurrentAction(){
      $pathArray = explode('/',Yii::app()->request->getPathInfo());
		if($pathArray[0] === '' || count($pathArray) == 1){
			return '';
		}else{
			return $pathArray[1];
			
		}
			
		
	}
	// 在 渲染页面之前时确定 静态资源的路由
	public function beforeRender($view){
		
	   if(Yii::app()->params['static'] === '')
	     Yii::app()->params['static'] = Yii::app()->baseUrl.'/';
		return true;
	}
	
	public function activateArray(){
	   if($this->isActivated == -1)
	    return;
	   else{
	     $this->activateArray[$this->isActivated] = 'active';
	   }
	}
	public function filterCheckLogin($filterChain){
				if (Yii::app()->user->isGuest)  
                  Yii::app()->user->loginRequired(); 
					$filterChain->run();
	}
	 public function filters(){
		     return array(
				'CheckLogin',
			    'accessControl',
			 );
		 }
		 
		 
	
    // 辅助方法，日志记录
     public function logAr($model){
	    Yii::log(CHtml::errorSummary($model).'#db Error#','error','Application.error');
	 }
	 // error log
	 public function logError($msg){
		 Yii::log($msg,'error','system.web');
	 }
	  
	 /* 输出 excel file
        before you use it please ensure that  
		   Yii::$enableIncludePath = false;
		   Yii::import('application.extensions.phpExcel.PHPExcel',1);
	 
	 */	 
    protected function downloadExcel($filename,$objPHPExcel){
		
		// Redirect output to a client’s web browser (Excel5)
		header('Content-Type: application/vnd.ms-excel');
		header('Content-Disposition: attachment;filename="'.$filename.'.xlsx"');
		header('Cache-Control: max-age=0');
		// If you're serving to IE 9, then the following may be needed
		header('Cache-Control: max-age=1');
		
		// If you're serving to IE over SSL, then the following may be needed
		header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
		header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
		header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
		header ('Pragma: public'); // HTTP/1.0
	
		$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
		$objWriter->save('php://output');
		
	}
	 /* 输出 Word  file
        
	 */	
	
    protected function downloadWord($filename,$phpWord){
		
		
		header("Content-Description: File Transfer");
		header('Content-Disposition: attachment; filename="' . $filename . '.docx"');
		header('Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document');
		header('Content-Transfer-Encoding: binary');
		header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
		header('Expires: 0');
		$xmlWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
		$xmlWriter->save("php://output");
		
	}    
	
	 
}